{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import os\n",
    "import math\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "\n",
    "sys.path.append(\"../utils/\")\n",
    "import date_util\n",
    "import token_util\n",
    "import enter_util\n",
    "import data_util\n",
    "import plot_util"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# trade_date = \"20190830\"\n",
    "# delta_price = 30.0\n",
    "# stock_list = data_util.get_stock_list(trade_date, delta_price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选出符合入场点的股票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = '20190801'\n",
    "test_choose_list = enter_util.get_condidate_stock(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_choose_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "choose_data = enter_util.enter_for_plot(test_choose_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enter_true, enter_false, enter_discount = plot_util.plot_enter(choose_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enter_discount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模拟交易"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = pd.read_csv(\"../data_pulled/day/000001.SZ.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method IndexOpsMixin.tolist of Index(['ts_code', 'trade_date', 'open', 'high', 'low', 'close', 'pre_close',\n",
       "       'change', 'pct_chg', 'vol', 'amount', 'diff', 'dea', 'macd', 'ema12',\n",
       "       'ema26'],\n",
       "      dtype='object')>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.columns.to_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Deal:\n",
    "    def __init__(self, stock_code=None, enter_date=None, enter_price=None):\n",
    "        \n",
    "        self.stock_code = stock_code\n",
    "        self.state = \"hold\"\n",
    "        self.enter_date = enter_date\n",
    "        self.enter_price = enter_price\n",
    "        self.out_date = None\n",
    "        self.out_price = None\n",
    "        \n",
    "        self.columns = ['stock_code', 'trade_date', 'open', 'high', 'low', 'close', 'pre_close', \n",
    "                        'change', 'pct_chg', 'vol', 'amount', 'diff', 'dea', 'macd', 'ema12', 'ema26']\n",
    "        self.stock_log = pd.DataFrame(columns = self.columns)\n",
    "        \n",
    "    def update_log(self, data):\n",
    "        if self.stock_log['trade_date'].isin(data[\"trade_date\"]).values.sum() == 0:\n",
    "            self.stock_log = self.stock_log.append(data).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "state = [\"hold\", \"selled\"]\n",
    "current_deal = [] # 记录正在进行的交易\n",
    "past_deal = [] # 记录已完成的交易\n",
    "all_log_columns = ['stock_code', 'name', 'state', 'enter_date', 'hold_days', 'out_date', 'buy_price',\n",
    "           'sell_price', 'pct_chg', 'pct_chg_each_day', 'amount', 'profit']\n",
    "all_stock_log = pd.DataFrame(columns = all_log_columns) # 记录每支股票的交易"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data_pulled/day/里面的数据, type(trade_date) = numpy.int64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = '20190801'\n",
    "end_date = '20190901'\n",
    "\n",
    "ROOT_PATH = os.path.join(\"..\", \"data_pulled\")\n",
    "ROOT_PATH = os.path.join(ROOT_PATH, \"day\")\n",
    "stock_csv_list = os.listdir(ROOT_PATH)\n",
    "\n",
    "work_date_list = date_util.get_work_day_range(start_date, end_date)\n",
    "\n",
    "for date in work_date_list:\n",
    "    \n",
    "    if len(current_deal) != 0:\n",
    "        for deal in current_deal:\n",
    "            # 更新持仓的数据\n",
    "            \n",
    "            stock_data = pd.read_csv(os.path.join(ROOT_PATH, deal.stock_code + \".csv\"))\n",
    "            data_today = stock_data[stock_data[\"trade_date\"]==deal.enter_date]\n",
    "            deal.update_log(data_today)\n",
    "        \n",
    "            # 出场(t+1)天\n",
    "            \n",
    "        \n",
    "        \n",
    "    # 选股\n",
    "    \n",
    "    choose_list = enter_util.get_condidate_stock(date)\n",
    "    stock_choosed = choose_list_tmp.iloc[0]\n",
    "    \n",
    "    # 进场(t+1天)\n",
    "    \n",
    "    stock_code = stock_choosed[\"stock_code\"]\n",
    "    enter_date = int(date) + 1\n",
    "    \n",
    "    stock_path = os.path.join(ROOT_PATH, stock_choosed[\"stock_code\"] + \".csv\")\n",
    "    file_exist = os.path.exists(stock_path)\n",
    "    if file_exist:\n",
    "        stock_data = pd.read_csv(stock_path)\n",
    "        data_enter = stock_data[stock_data[\"trade_date\"]==enter_date]\n",
    "    \n",
    "    deal = Deal(stock_code = stock_code,\n",
    "                enter_date = enter_date,\n",
    "                enter_price = data_enter['open']\n",
    "               )\n",
    "    deal.update_log(data_enter)\n",
    "    current_deal.append(deal)\n",
    "    \n",
    "    tmp_log = pd.DataFrame([[\n",
    "        stock_code, stock_choosed[\"name\"], state[0], enter_date, \"\", \"\", data_enter['open'],\n",
    "        \"\", \"\", \"\", 100, \"\"\n",
    "    ]], columns = all_log_columns)\n",
    "    all_stock_log = all_stock_log.append(tmp_log)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
